<template>
  <a-modal
    title="手动上传正样本"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
<!--      <a-form-item-->
<!--        label="目标网关"-->
<!--        :labelCol="labelCol"-->
<!--        :wrapperCol="wrapperCol"-->
<!--        has-feedback=""-->
<!--      >-->
<!--        <a-select-->
<!--          :showSearch="true"-->
<!--          optionFilterProp='children'-->
<!--          style="width: 100%"-->
<!--          placeholder="请选择网关"-->
<!--          @change="handleChange"-->
<!--          v-decorator="['gatewayId', {rules: [{ required: true, message: '请选择网关！' }]}]">-->
<!--          <a-select-option v-for="(item,i) in gatewayList" :key="i" :value="item.id">{{ item.gatewayName }}-->
<!--          </a-select-option>-->
<!--        </a-select>-->
<!--      </a-form-item>-->
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="模型文件"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
          style="text-align: left"
        >
          <a-upload
            name="modelFile"
            :multiple="true"
            :customRequest="customRequest"
            :showUploadList="false"
            accept=".wav"
          >
            <a-button> <a-icon type="upload" />上传文件</a-button>
          </a-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {  registerSampleModel } from '@/api/modular/main/bizfaultmodel/bizFaultModelManage'
  import { getUploadFileOriginalName } from '@/api/modular/system/fileManage'
  import {listGatewayChannel} from "@/api/modular/main/bizgatewaychannel/bizGatewayChannelManage";
  import {gatewayList} from "@/api/modular/main/bizgateway/bizGatewayManage";

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        faultGif: '',
        fileList: [],
        record: {},
        gatewayList: [],
        gatewayId:'',
        channel:''
      }
    },
    methods: {
      batchReg (record) {
        this.visible = true
        this.gatewayId = record.gatewayId
        this.channel = record.channel
      },
      getGatewayList () {
        gatewayList().then(res => {
          if (res.success) {
            this.gatewayList = res.data
          }
        })
      },
      edit (record) {
        this.visible = true
        this.record = record
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id
          })
        }, 100)
      },
      customRequest (data) {
        console.log(data)
        this.fileList = []
        const formData = new FormData()
        formData.append('file', data.file)
        getUploadFileOriginalName(formData).then((res) => {
          console.log(res)
          if (res.success) {
            this.$message.success('上传成功')
            this.modelFile = res.data
            this.fileList.push(res.data)
            console.log(res.toString)
            data.onSuccess(res, data.file)
          } else {
            this.$message.error('上传失败：' + res.message)
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (this.fileList.length > 0) {
            values.modelFile = this.fileList.toString()
          }
          const formData = new FormData()
          formData.append('gatewayId', this.gatewayId)
          formData.append('cannel', this.channel)
          formData.append('filePath', values.modelFile)
          this.confirmLoading = true
          registerSampleModel(formData).then((res) => {
            console.log(res)
            if (res.success) {
              this.$message.success(res.data)
              this.$emit('ok', values)
              this.visible = false
              this.confirmLoading = false
            } else {
              this.$message.error('注册失败：' + res.message)
              this.confirmLoading = false
            }
          })
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      },
      handleChange (values) {
        this.gatewayId = values
      },
    }
  }
</script>
